// 音频资源
import resourecs from './musicResourecs.js';
let musics = resourecs.musicResourecs;

let audio, timeout;
export default {
	state:{
		// 是否播放标识
		playStatus: false,
		// 当前歌曲标识
		currentPlayIndex: 0,
		// 音频总时长
		durationTime: 0,
		// 当前播放时间
		currentTime: 0
		
	},
	getters:{
		audioName(state){
			let curIndex = state.currentPlayIndex;
			return musics[curIndex].name;
		},
		singerName(state){
			let curIndex = state.currentPlayIndex;
			return musics[curIndex].singer.name;
		},
		singerSynopsis(state){
			let curIndex = state.currentPlayIndex;
			return musics[curIndex].singer.synopsis;
		}
	},
	mutations:{
		// 销毁音频播放器各个监听事件
		destruction(){
			audio.offPlay();
			audio.offPause();
			audio.offStop();
			audio.offEnded();
			audio.offTimeUpdate();
			audio.offError();
		},
		// 播放
		audioPlay(state){
			let curIndex = state.currentPlayIndex;
			audio.src = musics[curIndex].src;
			audio.play();
		},
		// 歌曲时间点跳转
		audioSeek(state, pos){
			audio.seek(pos);
		},
		// 暂停
		audioPause(){
			audio.pause();
		},
		// 停止歌曲
		audioStop(){
			audio.stop();
		},
		// 改变播放状态
		changePlayStatus(state,Boolean){
			console.log('Boolean:',Boolean);
			state.playStatus = Boolean;
		},
		// 改变当前播放歌曲
		changePlayIndex(state, index){
			state.currentPlayIndex = index;
		},
		getDurationTime(state, time){
			state.durationTime = time;
		},
		// 改变歌曲当前播放时间点
		changeCurrentTime(state, time){
			state.currentTime = time;
		}
	},
	actions:{
		// 初始化
		init({commit, dispatch}){
			if(audio){
				return;
			}
			// 通过API实例化audio对象
			audio = uni.createInnerAudioContext();
			
			// 监听音频播放器动作
			audio.onPlay(() => {
				commit('changePlayStatus', true);
				// 获取切歌后歌的时间段
				commit('getDurationTime', audio.duration);
				console.log('开始播放');
			});
			audio.onPause(() => {
				commit('changePlayStatus', false);
				console.log('暂停播放');
			});
			audio.onStop(() => {
				commit('changePlayStatus', false);
				// 歌曲播放完后自动切歌下一曲
				console.log('停止播放');
			});
			audio.onEnded(() => {
				commit('changePlayStatus', false);
				dispatch('preOrNext','next')
				console.log('播放结束');
			});
			// 音频时间点变化监听
			audio.onTimeUpdate(() => {
				commit('changeCurrentTime', audio.currentTime);
			});
			audio.onError((res) => {
				commit('changePlayStatus', false);
				console.log('播放错误');
				console.log(res.errMsg);
				console.log(res.errCode);
			});
		},
		// 播放与暂停
		playOrPause({state,commit}){
			if(!state.playStatus){
				commit('audioPlay');
			}else{
				commit('audioPause');
			}
		},
		// 切歌
		preOrNext({state,commit}, type){
			console.log('上一首下一首');
			// 先暂停之前的歌，再进行切歌
			commit('audioStop');
			
			// 切歌
			let curIndex = state.currentPlayIndex,
			lastIndex = musics.length - 1;
			
			switch (type){
				case 'pre':
				    curIndex === 0 ? commit('changePlayIndex', lastIndex) : commit('changePlayIndex',curIndex - 1);
					break;
				case 'next':
				    curIndex === lastIndex ? commit('changePlayIndex', 0) : commit('changePlayIndex',curIndex + 1);
					break;	
				default:
					break;
			}
			
			// 播放
			commit('audioPlay');
		},
		sliderToPlay({state,commit},{ detail: { value: position }}){
			// console.log('音频时间控制条:', position);
			// 选中的时间点传入并通过seek方法来跳转到该时间点
			let time = position;
			commit('audioSeek', time);
			if(!state.playStatus){
				// commit('audioPlay');
				// 简单节流
				clearTimeout(timeout);
				timeout = setTimeout(() => commit('changeCurrentTime', time), 200);
			}
		}
	}
}